//
// ***********************************************
// *    ___________   ___________  __      __    *
// *   |______  __/  /_____  ___/  \ \    / /    *
// *         / /          / /       \ \  / /     *
// *        / /          / /         \ \/ /      *
// *       / /          / /           \  /       *
// *      / /______    / /            / /        *
// *     /________/   /_/            /_/         *
// *                                             *
// ***********************************************
//
//   ViewController.m
//   welfareMallDemo
//   
//   Created  by mac on 2019/8/27
//   Modified by Tiyon
//   Copyright © 2019 wanmeizty. All rights reserved.
//   
   

#import "ViewController.h"

#define STATUS_BAR_HEIGHT (([[UIScreen mainScreen] bounds].size.height-812) >= 0 ? 44.0 : 20.0)

#define HOME_INDICATOR_HEIGHT (([[UIScreen mainScreen] bounds].size.height-812) >= 0 ? 34.f : 0.f)

#import <welfareMallSDK/DTSDKWebView.h>
#import "SelViewController.h"
@interface ViewController ()<DTApiDelegate,WKNavigationDelegate>
@property (strong,nonatomic) DTSDKWebView * webview;
@property (strong,nonatomic) DTSDKJsApi * jsApi;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self setUpWebview];
    
}


- (void)setUpWebview{
    
    
    
    [self.view addSubview:self.webview];
    [self.webview addJavascriptApi:self.jsApi namespace:nil];
    [self.webview setDebugMode:true];
    
    
    NSString * clientId = @"81c1b54196a046a78a4f78f917a8b469";
    NSString * clientSecret = @"1764a15d67714016abb08091bc866ba8f588bb76";
    NSString * uid = @"10082";
    //流量主服务端token，可根据实际情况决定是否需要此参数
    NSString * token = @"44bc250f38796ddce5809c5892db5795";
    
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
//    [dict setObject:uid forKey:@"uid"];
    [dict setObject:clientId forKey:@"client_id"];
    [dict setObject:clientSecret forKey:@"client_secret"];
    [dict setObject:token forKey:@"token"];
    [dict setObject:@"page.taolijin" forKey:@"type"];
    [dict setObject:uid forKey:@"uid"];
    [dict setObject:@"xxx" forKey:@"xxxxx"];
    // 授权url
    NSString * authUrl = [self.webview authorityUrl:dict environ:NO];
    [self.webview loadUrl:authUrl];
    
    UIButton *  btn = [[UIButton alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 80) * 0.5, 84, 80, 40)];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setTitle:@"配置" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(clickdd) forControlEvents:UIControlEventTouchUpInside];
    btn.backgroundColor = [UIColor greenColor];
    [self.view addSubview:btn];
}

-(void)clickdd{
    SelViewController * selvc = [[SelViewController alloc] init];
    __weak __typeof(self) weakSelf = self;
    selvc.finishBlock = ^(NSDictionary * _Nonnull param) {
        
        NSString * clientId = [param objectForKey:@"clientId"];
        NSString * clientSecret = [param objectForKey:@"clientSecret"];;
        NSString * uid = [param objectForKey:@"uid"];
        BOOL isrelease = [[param objectForKey:@"isrelease"] boolValue];
        
        //流量主服务端token，可根据实际情况决定是否需要此参数
//        NSString * token = @"44bc250f38796ddce5809c5892db5795";
        
        NSMutableDictionary * dict = [NSMutableDictionary dictionary];
//        [dict setObject:uid forKey:@"uid"];
        [dict setObject:clientId forKey:@"client_id"];
        [dict setObject:clientSecret forKey:@"client_secret"];
//        [dict setObject:token forKey:@"token"];
        [dict setObject:@"page.taolijin" forKey:@"type"];
        [dict setObject:@"xxx" forKey:@"xxxxx"];
        if (uid.length > 0) {
            [dict setObject:uid forKey:@"uid"];
        }
        // 授权url
        NSString * authUrl = [self.webview authorityUrl:dict environ:isrelease];
        [weakSelf.webview loadUrl:authUrl];
    };
    [self presentViewController:selvc animated:YES completion:nil];
}

/**
 * token失效
 **/
- (void)tokenFailed:(NSDictionary *)args{
    // 失败后重新授权
    NSLog(@"%@",args);
}

- (void)goBack{
    if ([self.webview canGoBack]) {
        [self.webview goBack];
    }
    //处理H5返回按钮的事件
}

-(void)consumeSuccess{
    //用户金币兑换淘礼金成功后后的回调
    [self.webview refresh];
}

-(void)earnGold{
    //客户端实现跳转用户赚金币页面
}


//- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
//    
//    decisionHandler(WKNavigationActionPolicyAllow);
//}


-(DTSDKWebView *)webview{
    if (!_webview) {
        _webview = [[DTSDKWebView alloc] initWithFrame:CGRectMake(0, STATUS_BAR_HEIGHT, self.view.frame.size.width, self.view.frame.size.height - STATUS_BAR_HEIGHT - HOME_INDICATOR_HEIGHT)];
//        _webview.navigationDelegate = self;
    }
    return _webview;
}

-(DTSDKJsApi *)jsApi{
    if (!_jsApi) {
        _jsApi = [[DTSDKJsApi alloc] init];
        _jsApi.delegate = self;
        
    }
    return _jsApi;
}


@end
